package com.ms.p2p.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.p2p.Constants;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.provider.MemberProvider;
import com.ms.p2p.plugin.validation.annotation.RequiredStringValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 用户中心
 **/
@Controller
@RequestMapping({"/member/ucenter"})
public class UcenterController extends BaseController {

    @Reference
    private MemberProvider memberProvider;

    /**
     * 修改登录密码
     */
    @Validations(requiredStrings = {
            @RequiredStringValidator(fieldName = "oldpass", message = "旧 密码不能为空"),
            @RequiredStringValidator(fieldName = "pass1", message = "就密码不能为空"),
            @RequiredStringValidator(fieldName = "pass2", message = "确认不能为空")
    })
    @RequestMapping("/modifyLoginPassword")
    @ResponseBody
    public Message modifyLoginPassword(String oldpass, String pass1, String pass2) {

        Member member = getCurrentUser();
        // 密码正则
        String pwdRegex = "^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]{6,20}$";

        if (StringUtils.isBlank(oldpass) || !StringUtils.equals(DigestUtils.md5Hex(oldpass),
                member.getLogin_password())) {
            return Message.error("旧密码输入不正确");
        }
        if (StringUtils.isBlank(pass1)) {
            return Message.error("新密码不能为空！");
        }
        if (!StringUtils.equals(pass1, pass2)) {
            return Message.error("两次输入的密码不一致！");
        }
        if (!pass1.matches(pwdRegex)) {
            return Message.error("密码必须6-20位,必须包含字母、数字、其他字符组合");
        }
        member.setLogin_password(DigestUtils.md5Hex(pass1));
        member.setUp_time(String.valueOf(System.currentTimeMillis()));
        member.setUp_ip(AppContext.getRemoteIp());
        memberProvider.updateById(member);
        return Message.success("密码重置成功，请重新登陆！");
    }

    /**
     * 修改支付密码
     */
    @Validations(requiredStrings = {
            @RequiredStringValidator(fieldName = "oldpass", message = "旧 密码不能为空"),
            @RequiredStringValidator(fieldName = "pass1", message = "就密码不能为空"),
            @RequiredStringValidator(fieldName = "pass2", message = "确认不能为空")

    })
    @RequestMapping("/modifyPayPassword")
    @ResponseBody
    public Message modifyPayPwd(String oldpass, String pass1, String pass2) {
        Member member = getCurrentUser();
        member = memberProvider.findById(member.getId());
        // 密码正则
        String pwdRegex = "^(.*){6,20}$";

        if (StringUtils.isBlank(pass1)) {
            return Message.error("新密码不能为空！");
        }

        if (StringUtils.isBlank(oldpass) || !StringUtils.equals(DigestUtils.md5Hex(oldpass),
                member.getPayment_password())) {
            return Message.error("原交易密码输入不正确");
        }


        if (!StringUtils.equals(pass1, pass2)) {
            return Message.error("两次输入的密码不一致！");
        }

        if (!pass1.matches(pwdRegex)) {
            return Message.error("密码必须6-20位");
        }

        if (member.getLogin_password().equals(
                DigestUtils.md5Hex(pass1))) {
            return Message.error("交易密码不能和登陆密码相同！");
        }

        member.setPayment_password(DigestUtils.md5Hex(pass1));
        member.setUp_time(String.valueOf(System.currentTimeMillis()));
        member.setUp_ip(AppContext.getRemoteIp());
        memberProvider.updateById(member);

        return Message.success("密码重置成功，请重新登陆！");
    }


    /**
     * 修改交易密码
     */
    @RequestMapping(value = "/forgetPaypass")
    @ResponseBody
    public Message forgetPaypass(String pass1, String pass2, String mobilecode) {

        String mobileCode = (String) AppContext.getSessionAttr(Constants.COMMON_MOBILE_CODE_SESSION_KEY);
        if (StringUtils.isBlank(mobileCode) || !StringUtils.equals(mobilecode, mobileCode)) {
            throw new NoticeException("验证码错误或已过期无效");
        }

        if (StringUtils.isBlank(pass1)) {
            throw new NoticeException("密码不能为空");
        }

        if (!pass1.equals(pass2)) {
            throw new NoticeException("两次密码输入不一致");
        }

        Member user = memberProvider.findByUserName(getCurrentUser().getUser_name());
        user.setPayment_password(DigestUtils.md5Hex(pass1));
        user.setUp_time(String.valueOf(System.currentTimeMillis()));
        user.setUp_ip(AppContext.getRemoteIp());

        AppContext.removeAttribute(Constants.COMMON_MOBILE_CODE_SESSION_KEY);
        memberProvider.updateById(user);
        AppContext.setSessionAttr("loginUser", user);
        return Message.success("修改成功");
    }

}
